package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author MrZhao
 * @created 2025-10-07 18:13
 * @description 用户管理Controller
 *
 */
@RestController
@RequestMapping(path = "/users")
@SuppressWarnings("all")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids){
          userService.deleteBatch(ids);
          return Result.success();
    }

    // 根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }
}
